//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MultiplicativePriceSeasonality.h"
using namespace Cephei::QL::Termstructures::Inflation;
#include <gen/QL/Termstructures/InflationTermStructure.h>
#include <gen/QL/Termstructures/Inflation/Seasonality.h>
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::CMultiplicativePriceSeasonality (DateTime seasonalityBaseDate, QL::Times::FrequencyEnum frequency, Cephei::Core::IVector<Double>^ seasonalityFactors) : CSeasonality(CMultiplicativePriceSeasonality::typeid)
{
    try
    {
#ifdef HANDLE
        _phMultiplicativePriceSeasonality = NULL;
#endif
        QuantLib::Date _seasonalityBaseDate = (QuantLib::Date)ValueHelper::Convert (seasonalityBaseDate); //d
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        seasonalityFactors->Lock();
        INativeVector<Double>^ _NCIseasonalityFactors = seasonalityFactors->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCseasonalityFactors = safe_cast<CDoubleVector^>(_NCIseasonalityFactors);
        std::vector<QuantLib::Rate> _seasonalityFactors = static_cast<std::vector<QuantLib::Rate>> (_NCseasonalityFactors->GetReference ());
        _ppMultiplicativePriceSeasonality = new boost::shared_ptr<QuantLib::MultiplicativePriceSeasonality> (new QuantLib::MultiplicativePriceSeasonality ( _seasonalityBaseDate,  _frequency,  _seasonalityFactors ));
        SetSeasonality (boost::dynamic_pointer_cast<QuantLib::Seasonality> (*_ppMultiplicativePriceSeasonality));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (seasonalityFactors != nullptr) seasonalityFactors->Unlock();    //not optional
    }
}
Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::CMultiplicativePriceSeasonality () : CSeasonality(CMultiplicativePriceSeasonality::typeid)
{
    try
    {
#ifdef HANDLE
        _phMultiplicativePriceSeasonality = NULL;
#endif
        _ppMultiplicativePriceSeasonality = new boost::shared_ptr<QuantLib::MultiplicativePriceSeasonality> (new QuantLib::MultiplicativePriceSeasonality ( ));
        SetSeasonality (boost::dynamic_pointer_cast<QuantLib::Seasonality> (*_ppMultiplicativePriceSeasonality));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::CMultiplicativePriceSeasonality (boost::shared_ptr<QuantLib::MultiplicativePriceSeasonality>& childNative, Object^ owner) : CSeasonality(CMultiplicativePriceSeasonality::typeid)
{
#ifdef HANDLE
	_phMultiplicativePriceSeasonality = NULL;
#endif
	_ppMultiplicativePriceSeasonality = &childNative;
    _ppSeasonality = new boost::shared_ptr<QuantLib::Seasonality> (boost::dynamic_pointer_cast<QuantLib::Seasonality> (*_ppMultiplicativePriceSeasonality));
}
Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::CMultiplicativePriceSeasonality (QuantLib::MultiplicativePriceSeasonality& childNative, Object^ owner) : CSeasonality(CMultiplicativePriceSeasonality::typeid)
{
#ifdef HANDLE
	_phMultiplicativePriceSeasonality = NULL;
#endif
	_ppMultiplicativePriceSeasonality = new boost::shared_ptr<QuantLib::MultiplicativePriceSeasonality> (&childNative);
    _ppSeasonality = new boost::shared_ptr<QuantLib::Seasonality> (boost::dynamic_pointer_cast<QuantLib::Seasonality> (*_ppMultiplicativePriceSeasonality));
    _MultiplicativePriceSeasonalityOwner = owner;
    _SeasonalityOwner = owner;
}

Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::CMultiplicativePriceSeasonality (CMultiplicativePriceSeasonality^ copy) : CSeasonality(CMultiplicativePriceSeasonality::typeid)
{
#ifdef HANDLE
	_phMultiplicativePriceSeasonality = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMultiplicativePriceSeasonality = new boost::shared_ptr<QuantLib::MultiplicativePriceSeasonality> (copy->GetShared());
        _ppSeasonality = new boost::shared_ptr<QuantLib::Seasonality> (boost::dynamic_pointer_cast<QuantLib::Seasonality> (*_ppMultiplicativePriceSeasonality));
    }
}
Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::CMultiplicativePriceSeasonality (PLATFORM::Type^ t) : CSeasonality(CMultiplicativePriceSeasonality::typeid)
{
#ifdef HANDLE
	_phMultiplicativePriceSeasonality = NULL;
#endif
	if (!t->IsSubclassOf(CMultiplicativePriceSeasonality::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::CMultiplicativePriceSeasonality (QuantLib::Handle<QuantLib::MultiplicativePriceSeasonality>& childNative, Object^ owner)  : CSeasonality(CMultiplicativePriceSeasonality::typeid)
{
	_phMultiplicativePriceSeasonality = &childNative;
	_ppMultiplicativePriceSeasonality = &static_cast<boost::shared_ptr<QuantLib::MultiplicativePriceSeasonality>>(childNative.currentLink());
    _ppSeasonality = new boost::shared_ptr<QuantLib::Seasonality> (boost::dynamic_pointer_cast<QuantLib::Seasonality> (*_ppMultiplicativePriceSeasonality));
    _MultiplicativePriceSeasonalityOwner = owner;
}
Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::CMultiplicativePriceSeasonality (QuantLib::Handle<QuantLib::MultiplicativePriceSeasonality> childNative)  : CSeasonality(CMultiplicativePriceSeasonality::typeid)
{
	_phMultiplicativePriceSeasonality = &childNative;
	_ppMultiplicativePriceSeasonality = &static_cast<boost::shared_ptr<QuantLib::MultiplicativePriceSeasonality>>(childNative.currentLink());
    _ppSeasonality = new boost::shared_ptr<QuantLib::Seasonality> (boost::dynamic_pointer_cast<QuantLib::Seasonality> (*_ppMultiplicativePriceSeasonality));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::CMultiplicativePriceSeasonality (QuantLib::MultiplicativePriceSeasonality childNative)  : CSeasonality(CMultiplicativePriceSeasonality::typeid)
{
#ifdef HANDLE
	_phMultiplicativePriceSeasonality = NULL;
#endif
	_ppMultiplicativePriceSeasonality = new boost::shared_ptr<QuantLib::MultiplicativePriceSeasonality> (new QuantLib::MultiplicativePriceSeasonality (childNative));
    _ppSeasonality = new boost::shared_ptr<QuantLib::Seasonality> (boost::dynamic_pointer_cast<QuantLib::Seasonality> (*_ppMultiplicativePriceSeasonality));
}
#endif

Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::~CMultiplicativePriceSeasonality ()
{
    if (_ppMultiplicativePriceSeasonality != NULL)
    {
	    delete _ppMultiplicativePriceSeasonality;
        _ppMultiplicativePriceSeasonality = NULL;
    }
}
Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::!CMultiplicativePriceSeasonality ()
{
    if (_ppMultiplicativePriceSeasonality != NULL)
    {
	    delete _ppMultiplicativePriceSeasonality;
    }
}
QuantLib::MultiplicativePriceSeasonality& Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::GetReference ()
{
    if (_ppMultiplicativePriceSeasonality == NULL) throw REFNEW NativeNullException ();
	return **_ppMultiplicativePriceSeasonality;
}
boost::shared_ptr<QuantLib::MultiplicativePriceSeasonality>& Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::GetShared ()
{
    if (_ppMultiplicativePriceSeasonality == NULL) throw REFNEW NativeNullException ();
	return *_ppMultiplicativePriceSeasonality;
}
QuantLib::MultiplicativePriceSeasonality* Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::GetPointer ()
{
    if (_ppMultiplicativePriceSeasonality == NULL) throw REFNEW NativeNullException ();
	return &**_ppMultiplicativePriceSeasonality;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MultiplicativePriceSeasonality>& Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::GetHandle ()
{
	if (_phMultiplicativePriceSeasonality == NULL)
	{
		_phMultiplicativePriceSeasonality = new Handle<QuantLib::MultiplicativePriceSeasonality> (*_ppMultiplicativePriceSeasonality);
	}
	return *_phMultiplicativePriceSeasonality;
}
#endif
bool Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::HasNative () 
{
	return (_ppMultiplicativePriceSeasonality != NULL);
}

QL::Times::FrequencyEnum Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::Frequency::get ()
{
    try
    {
    	QuantLib::Frequency _rv = (QuantLib::Frequency)(*_ppMultiplicativePriceSeasonality)->frequency ( );   
        QL::Times::FrequencyEnum _nrv = (QL::Times::FrequencyEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::SeasonalityBaseDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppMultiplicativePriceSeasonality)->seasonalityBaseDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::SeasonalityFactor (DateTime d)
{
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppMultiplicativePriceSeasonality)->seasonalityFactor ( _d );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::CorrectYoYRate (DateTime d, Double r, Cephei::QL::Termstructures::IInflationTermStructure^ iTS)
{
    CInflationTermStructure^ _CiTS;
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
        QuantLib::Rate _r = (QuantLib::Rate)ValueHelper::Convert (r); //a
        _CiTS = safe_cast<CInflationTermStructure^> (iTS);
        _CiTS->Lock();
        QuantLib::InflationTermStructure& _iTS = static_cast<QuantLib::InflationTermStructure&> (_CiTS->GetReference ()); 
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppMultiplicativePriceSeasonality)->correctYoYRate ( _d,  _r,  _iTS );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CiTS != nullptr) _CiTS->Unlock();
    }
}
Double Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::CorrectZeroRate (DateTime d, Double r, Cephei::QL::Termstructures::IInflationTermStructure^ iTS)
{
    CInflationTermStructure^ _CiTS;
    try
    {
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
        QuantLib::Rate _r = (QuantLib::Rate)ValueHelper::Convert (r); //a
        _CiTS = safe_cast<CInflationTermStructure^> (iTS);
        _CiTS->Lock();
        QuantLib::InflationTermStructure& _iTS = static_cast<QuantLib::InflationTermStructure&> (_CiTS->GetReference ()); 
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppMultiplicativePriceSeasonality)->correctZeroRate ( _d,  _r,  _iTS );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CiTS != nullptr) _CiTS->Unlock();
    }
}
Boolean Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::IsConsistent (Cephei::QL::Termstructures::IInflationTermStructure^ iTS)
{
    CInflationTermStructure^ _CiTS;
    try
    {
        _CiTS = safe_cast<CInflationTermStructure^> (iTS);
        _CiTS->Lock();
        QuantLib::InflationTermStructure& _iTS = static_cast<QuantLib::InflationTermStructure&> (_CiTS->GetReference ()); 
    	bool _rv = (bool)(*_ppMultiplicativePriceSeasonality)->isConsistent ( _iTS );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CiTS != nullptr) _CiTS->Unlock();
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::SeasonalityFactors::get ()
{
    try
    {
    	std::vector<QuantLib::Rate> _rv = (std::vector<QuantLib::Rate>)(*_ppMultiplicativePriceSeasonality)->seasonalityFactors ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Inflation::IMultiplicativePriceSeasonality^ Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality::Set (DateTime seasonalityBaseDate, QL::Times::FrequencyEnum frequency, Cephei::Core::IVector<Double>^ seasonalityFactors)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _seasonalityBaseDate = (QuantLib::Date)ValueHelper::Convert (seasonalityBaseDate); //a
        QuantLib::Frequency _frequency = (QuantLib::Frequency)frequency ;
        seasonalityFactors->Lock();
        INativeVector<Double>^ _NCIseasonalityFactors = seasonalityFactors->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCseasonalityFactors = safe_cast<CDoubleVector^>(_NCIseasonalityFactors);
        std::vector<QuantLib::Rate> _seasonalityFactors = static_cast<std::vector<QuantLib::Rate>> (_NCseasonalityFactors->GetReference ());
    	(*_ppMultiplicativePriceSeasonality)->set ( _seasonalityBaseDate,  _frequency,  _seasonalityFactors );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (seasonalityFactors != nullptr) seasonalityFactors->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Inflation::IMultiplicativePriceSeasonality^ Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality_Factory::Create (DateTime seasonalityBaseDate, QL::Times::FrequencyEnum frequency, Cephei::Core::IVector<Double>^ seasonalityFactors)
{
    return REFNEW CMultiplicativePriceSeasonality ( seasonalityBaseDate,  frequency,  seasonalityFactors);
}
Cephei::QL::Termstructures::Inflation::IMultiplicativePriceSeasonality^ Cephei::QL::Termstructures::Inflation::CMultiplicativePriceSeasonality_Factory::Create ()
{
    return REFNEW CMultiplicativePriceSeasonality ();
}
